package net.onecloud.onecloud.employ;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.adapter.EmployAdapter;
import net.onecloud.onecloud.base.BasePage;
import net.onecloud.onecloud.model.ResultEmploy;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.RecycleViewDivider;


import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;

import rx.schedulers.Schedulers;


/**
 * Created by zaizai on 16/7/26.
 */
public class EmployPage extends BasePage implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {

    public static final String POST = "post";
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private EmployAdapter mEmployAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private Subscription subscription;
    private int page = 1;

    private Observer<ResultEmploy> moreObserver = new Observer<ResultEmploy>() {
        @Override
        public void onCompleted() {
            LogUtils.d("请求完成");

        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);
        }

        @Override
        public void onNext(ResultEmploy resultEmploy) {
            if (resultEmploy != null && resultEmploy.getData() != null && resultEmploy.getData().size() > 0) {
                if (resultEmploy.getMore() == 1) {
                    page = page + 1;
                } else {
                    page = -1;
                }
                mEmployAdapter.notifyDataChangedAfterLoadMore(resultEmploy.getData(), true);
            }

        }
    };
    private Observer<ResultEmploy> updateObserver = new Observer<ResultEmploy>() {

        @Override
        public void onCompleted() {
            LogUtils.d("请求完成");


        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);

        }

        @Override
        public void onNext(ResultEmploy resultEmploy) {
            mEmployAdapter.setNewData(resultEmploy.getData());
            page += 1;
            mSwipeRefreshLayout.setRefreshing(false);
        }
    };


    public EmployPage(Activity activity) {
        super(activity);
        initView();
        initListener();

    }

    private void initListener() {
        mEmployAdapter.setOnLoadMoreListener(this);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mEmployAdapter.setOnRecyclerViewItemClickListener(this);
    }

    public void initView() {
        View view = View.inflate(mActivity, R.layout.page_employ, null);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.m_swiperefresh);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.m_recyclerview);
        mEmployAdapter = new EmployAdapter();
        mLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(mActivity, LinearLayoutManager.VERTICAL));
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mEmployAdapter);
        this.mFrameLayoutContent.addView(view);
    }

    @Override
    public void initData() {
        LogUtils.d("start");
        updateRV();
    }

    private void updateRV() {
        mEmployAdapter.openLoadMore(10, true);
        page = 1;
        subscription = NetWork.getEmployApi().queryListEmploy(page, 10)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mSwipeRefreshLayout.setRefreshing(true);
                    }
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(updateObserver);
    }

    @Override
    public void onLoadMoreRequested() {
        if (page >= 1) {
            subscription = NetWork.getEmployApi().queryListEmploy(page, 10)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(moreObserver);
        } else {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mEmployAdapter.notifyDataChangedAfterLoadMore(false);
                }
            }, 1000);
        }

    }

    @Override
    public void onRefresh() {
        updateRV();

    }

    @Override
    public void onItemClick(View view, int i) {
        //recycrView Item been onclick
        Bundle bundle = new Bundle();
        bundle.putParcelable(EmployPage.POST, (ResultEmploy.DataEntity) view.getTag());
        Intent intent = new Intent(mActivity, PostDetailListPage.class);
        intent.putExtras(bundle);
        mActivity.startActivity(intent);
    }
}
